using softDemo.Demain.User;
using System;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using softDemo.Demain.Responsity;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Authorization;
using Soft1ApiDemo.Infrastructure.Parameter;
using Soft1ApiDemo.Infrastructure.Utils;
using Microsoft.Extensions.Configuration;

namespace softDemo.API.Controllers
{
    [Authorize]
    [ApiController]
    [Route("[controller]")]
    public class UserController : ControllerBase
    {


        //定义类型为User接口的_user属性
        private readonly IResponsity<User> _user;
        private readonly IConfiguration _configuration;

        //构造函数
        public UserController(IResponsity<User> user, IConfiguration configuration)
        {
            _user = user;
            _configuration = configuration;
        }
        [HttpPost]
        [Route("add")]
        public dynamic Index(UserCot usercot)
        {
            // 把下面的抛出异常的注释去掉，可以测试全局异常过滤器是否有用，如果有用，则该异常就会被写入数据库中。
            // throw new Exception("你有一个bug还没有解决呢？");
            //添加
            var user = new User
            {
                Name = usercot.Name,
                Age = usercot.Age,
                Sex = usercot.Sex,
                IsDelete = usercot.IsDelete,
                UpdateAt = DateTime.Now,
                CreateAt = DateTime.Now
            };
            _user.Add(user);
            return new
            {
                Code = 1000,
                massagetion = "添加成功",
                data = ""
            };
        }

        public class UserCot
        {
            public string Name { get; set; }
            public int Age { get; set; }
            public char Sex { get; set; }
            public bool IsDelete { get; set; }
        }

        [HttpDelete]
        [Route("delete/{Id}")]
        //删除
        public dynamic Delete(Guid Id)
        {
            var BOOl = _user.Delete(Id);
            System.Console.WriteLine(BOOl);
            if (BOOl)
            {
                return new
                {
                    Code = 1000,
                    maggation = "删除成功",
                    ID = Id
                };
            }
            return new
            {
                Code = 404,
                maggation = "没有可以删除的数据了",
                ID = Id
            };
        }

        // 修改、更新
        [HttpPut]
        [Route("update/{Id}")]
        public dynamic Update(Guid Id, UserCot obj)
        {
            //根据Id获取需要更新的实体数据
            var entity = _user.GetById(Id);
            if (obj != null)
            {
                System.Console.WriteLine(obj);
                entity.IsDelete = obj.IsDelete;
                _user.Update(entity);
                return new
                {
                    Code = 1000,
                    maggation = "更新成功",
                    data = ""
                };
            }
            return new
            {
                Code = 404,
                maggation = "更新失败",
                data = ""
            };
        }

        [HttpGet]
        [Route("checkList")]
        //查找,获取整个表的所有数据
        public dynamic CheckList()
        {
            var entitys = _user.GetAble();
            return entitys;
        }


        [HttpGet]
        [Route("check/{Id}")]
        //查找,根据ID查找单个实体数据
        public dynamic Check(Guid Id)
        {
            var entity = _user.GetById(Id);
            if (entity != null)
            {
                return entity;
            }
            else
            {
                return new
                {
                    Code = 204,
                    maggation = "数据不存在,请确认你的ID是否真实存在",
                    data = ""
                };
            }
        }

        [AllowAnonymous]
        [Route("/token")]
        public string Login()
        {
            var username = "admin";
            var rolename = "超级管理";
            // var password="113";

            var tokenParameter = _configuration.GetSection("TokenParameter").Get<TokenParameter>();

            var token = TokenHelper.GenerateToken(tokenParameter, username, rolename);
            var res = new
            {
                Code = 1000,
                Msg = "登录成功",
                Data = token
            };
            return res.JsonSerialize();
        }


    }
}